#silkroad BMS parser

import os
from Sanae3D.lib.ModelObject import Model3D
from Sanae3D.lib.StructReader import StructReader

class silkroadBMS(Model3D):
   
   def __init__(self, obj3D=None, inFile=None, outFile=None):
      '''Initialize a Silkroad MSH Object.'''
      
      super(silkroadBMS, self).__init__("silkroadBMS")
      self.obj3D = obj3D
      self.inFile = inFile
      self.outFile = outFile
      
   def parse_file(self):
      
      filetype = self.inFile.read_string(8)
      version = self.inFile.read_string(4)
      if version == "0110":
         
         offVerts, offBones, offFaces, unkOff1, unkOff2, ofsbbox, unkOff4, \
                 unk1, unk2, unkOff5, unk3, unk4, unk5, unk6, unk7 \
                 = self.inFile.read_long(15)

         vertArray = []
         normArray = []
         uvArray = []
          
         self.inFile.seek(0x48)
         
         #get mesh group
         charCount = self.inFile.read_long()
         meshName =  self.inFile.read_string(charCount)
         self.create_object(meshName)
         
         #get material library
         charCount = self.inFile.read_long()
         
         matLib = self.inFile.read_string(charCount)
         self.create_material(0)
         self.add_material_name(0, matLib)
         self.add_texture_name(0, matLib + ".png")
         
         #unknown int
         unk1 = self.inFile.read_long()
         
         numVerts = self.inFile.read_long()
         print "%d vertices" %numVerts
         for i in range(0, numVerts):
            vx, vy, vz = self.inFile.read_float(3)
            nx, ny, nz = self.inFile.read_float(3)
            tu, tv = self.inFile.read_float(2)
						
	    vx = float(vx) * -1.0
            
            if unk6 == 0:
               self.inFile.seek(12, os.SEEK_CUR)
            elif unk6 == 1024:
               self.inFile.seek(20, os.SEEK_CUR)
         
            self.create_vertex(meshName, i)
            self.add_coords(meshName, i, [vx, vy, vz])
            self.add_vert_normals(meshName, i, [nx, ny, nz])
            self.add_vert_uv(meshName, i, [tu, tv])
            
         #bone count (ignore bones for now)
         self.inFile.seek(offBones, os.SEEK_SET)
         numBones = self.inFile.read_long()
         print "%d bones" %numBones
         
	 for i in range(numBones):
	    
	    #bone name
	    charCount = self.inFile.read_long()
	    boneName = self.inFile.read_string(charCount)
	    self.create_bone(i)
	    self.add_bone_name(i, boneName)
      
	 for i in range(numVerts):
	    b1 = self.inFile.read_byte()
	    w1 = self.inFile.read_short()
	    b2 = self.inFile.read_byte()
	    w2 = self.inFile.read_short()
	    
	    #self.add_vert_bones(meshName, i, [b1, b2])
	    #self.add_vert_weights(meshName, i, [w1, w2])
            
         #faces
         self.inFile.seek(offFaces, os.SEEK_SET)
         faceCount = self.inFile.read_long()
         print "%d faces" %faceCount
         for i in range(0, faceCount):
            f1, f2, f3 = self.inFile.read_short(3)
            
            self.create_face(meshName, i)
            self.add_face_verts(meshName, i, [f1, f2, f3])
            self.add_face_material(meshName, i, 0)
	    
	 self.inFile.seek(ofsbbox)
	 bbMin = self.inFile.read_float(3)
	 bbMax = self.inFile.read_float(3)
   
def read_file(path):
   
   openFile = StructReader(open(path, "rb"))
   obj = silkroadBMS(inFile=openFile)
   obj.parse_file()
   openFile.close()
   return obj

def definitions():
   
   return "JMXVBMS", "BMS", "Silkroad BMS"

if __name__ == '__main__':
   
   bms = "cj_thiefvill_02_02.bms"
   file1 = "cj_thiefvill_house_01_06.bms"
   file2 = "bow_04.bms"
   obj = read_file(file2)